Arrays

Objectives


Discussion

What is an Array?

0 1 2 3 4
"Hi"     "bye"   "3"     "asdf" "VB"   

Declaring and Using Arrays 

Resizing an Array

Back to top


Demonstration

In this demo we will create and manipulate arrays.

1.  Create a new project for Unit5. 

2.  On the form add a button (btnExperiment).

3.  We are going to create arrays, assign values, redim them, and display them.  It would be useful to have a subroutine to display our arrays which we can call whenever we want.  We will create one now.  Notice in the sub that we are passing an array-type into the sub.  This array type will let us pass any kind of array into the sub.  Also notice the length property of the array specifies the number of items in the array.  To get the index of the last item we use length - 1.

Private Sub DisplayArray(ByVal ArrayX As Array)
   Dim i As Int16
   Dim sList As String
   For i = 0 To ArrayX.Length - 1
      sList = sList & ArrayX(i) & vbCrLf 
   Next
   MessageBox.Show(sList)
End Sub

4.  Now we will create an array and initialize it, and redim it a few times.  In the click event for btnExperiment add the following code:

Dim sWords() As String = {"hi", "bye", "3"}
DisplayArray(sWords)
ReDim Preserve sWords(5)
sWords(4) = "new guy"
sWords(5) = "lastguy"
DisplayArray(sWords)
ReDim sWords(7)
DisplayArray(sWords)

5.  Test the program.  Study this code and the messageboxes.  When the array is first declared it has three items (items, 0,1,2).  When we redim it, it now has six items (items 0,1,2,3,4, 5).  Since we preserve it, the existing values are retained.  Then we add "new guy" to item 4 and "last guy" to item 5.  Notice that item 5 is the last item and so there is a blank in the messagebox where item 3 should be since no item 3 was added.  Finally we redim the array again only without the preserve keyword.  Now all of the information in the array is lost.

6.  Now add the following line of code at the bottom of the code in the click event for btnExperiment:

MessageBox.Show(sWords(8))

7.  Run the program. You should get an error that looks like: Index was outside the bounds of the array.  This error means that you referenced an element that does not exist.  When you used redim to resize the array you gave it 8 items (0 to 7) so there is no item 8.

8.  One of the benefits of using an array is that you can use for-loops to assign and display data easily for large arrays.  Now we will do one more example to show how we can easily assign values to an array.  Add another button and add the following code to the click event for this second button:

Dim iNums(20) As Int16
Dim i As Int16
For i = 0 To iNums.GetUpperBound(0)
   iNums(i) = 2 * i
Next
DisplayArray(iNums)

9.  Notice in the code that we use the GetUpperBound method.  This method returns the index of the last item in the array.  We need to pass the "0" in because the method needs to know which dimension to get the upper bound for.  In a one-dimensional array there is only one dimension which is dimension 0.  In the next lesson we will discuss arrays that have more than one dimension.

10. Run the program and check out the results.

Back to top


Exercises

1.  Write a statement to declare an array to hold 5 integers.

2.  Write a statement to declare an initialize an array to hold the 3 strings: "First", "Second", "Third".

3.  Assume that you have an array called sNames.  Write a statement to assign "Pat" to the sixth element.

4.  Assume that you have an array called dTemperatures.  Write a statement to display the fifth item.

5.  You have an array that has five items in it.  Write a statement to resize the array to hold six items without losing the current information.

6.  Write code to declare an array to have 100 integers and then initialize each element to a value of 10.

7.  Write a function that accepts an array of number and returns the sum of all the elements in the array.  

8.  Write a program to demonstrate your function in #7.  In your program you should define an array of numbers, then pass it into your function, and then display the result.

9. Write a program that allows the user to add items to an array, display the items in the array, and display the sum of the items in the array.  Your program should meet the following requirements:

  1. It should have an array to hold numbers.  Since you do not know how many items the user is going to enter you should probably declare it to hold 1 item and then redim it everytime the user adds a new number.  But you can also declare it so that it holds alot of numbers and then limit the user to that number of numbers.  This latter approach wastes memory but will work.  Also you probably should declare the array outside of all subs and functions and make it global to the form since several subs and functions will need to access it.
  2. It should have a textbox and a button to permit the user to enter numbers.  When the user clicks the button the number in the textbox should be added to the array.
  3. It should ensure that the item in the textbox is a number.
  4. It should provide a means to display all of the items in the array.
  5. It should provide a means to add all the items in the array and display the sum.
  6. It should enable the user to change the value of an item in the array.
Back to top
Links & Help
Back to top